﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Project.Interfaces;
using Project.Database;
using Project.Database.Repository;

namespace Project.Implementations.ClickHandler
{
    public class DefaultClickHandler : IClickHandler
    {
        private DatabaseRepository entities = new DatabaseRepository();

        public bool ProcessClick(int userID, int advertID, int blogID, string IP, DateTime timestamp)
        {
            if (entities.IsClickValid(advertID, blogID, IP, timestamp))
            {
                
                Advert ad = entities.GetAdvert(advertID);
                int err = entities.ReduceBalance(ad.AdCampain, ad.CostToAdvertiser);
                if (err != -1)
                {
                    entities.CreateClick(advertID, blogID, IP, timestamp);
                }
                if (err == 1)
                {
                    // SEND EMAIL WARNING
                    Console.WriteLine("Warning! Your balance is low: " + ad.AdCampain.Name + " " + ad.AdCampain.RemainingBalance);
                }
                if (err == -1)
                {
                    // SEND EMAIL WARNING AND REMOVE AD
                    Console.WriteLine("Warning! Your balance is zero: " + ad.AdCampain.Name + " " + ad.AdCampain.RemainingBalance);
                }
                return true;
            }
            else
            {
                // PROCESS INVALID CLICK
                Console.WriteLine("Invalid click from " + IP + " at " + timestamp);
            }
            return false;
        }
    }
}